﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TcpLabCommon;
using System.Data;
using System.Diagnostics;
using System.Threading;

namespace Common.GeneralClass
{
    public class ACQDB
    {
        public long Id;
        public string acqId;
        public string acqName;
        public  int testPointId;
        public CurrentDataPacketAndFFTData dataPaket;
        public DateTime insertTime;
        public int userId;
        public int projectId;
        public int testItemId;
        public int taskId;
        public int State;

    }



    /// <summary>
    /// 位移
    /// </summary>
    public class ACQData
    {
        public int channelid;
        public List<float> rpm = new List<float>();
        public List<float> GapVoltage = new List<float>();
        public List<float> RoomSquareValue = new List<float>();
        public List<float> OneMultiplierAmp = new List<float>();
        public List<float> OneMultiplierPhase = new List<float>();
        public List<float> TWOMultiplierAmp = new List<float>();
        public List<float> TWOMultiplierPhase = new List<float>();
        public List<DateTime> insertTime = new List<DateTime>();
        public List<float> PK2PK = new List<float>();
    }

    /// <summary>
    /// 位移
    /// </summary>
    public class ACQData_Field
    {
        public int channelid;
        public float Rpms ;
        public float FieldValues;
        public float FieldValues2;
        public DateTime date1;
        //public List<float> RoomSquareValue = new List<float>();
        //public List<float> OneMultiplierAmp = new List<float>();
        //public List<float> OneMultiplierPhase = new List<float>();
        //public List<float> TWOMultiplierAmp = new List<float>();
        //public List<float> TWOMultiplierPhase = new List<float>();
        //public List<DateTime> insertTime = new List<DateTime>();
        //public List<float> PK2PK = new List<float>();
    }

    //时间和转速
    public class SpeedToTime 
    {
        public int id;
        public List<DateTime> insertTime=new List<DateTime> ();
        public List<float> rpm = new List<float>();
        public List<float> SlowSignal = new List<float>();
        public List<float> Voltage = new List<float>();
    }
    /// <summary>
    /// 慢信号
    /// </summary>
    public class ACQSlow
    {
        public int id;
        public List<DateTime> insertTime = new List<DateTime>();
        public List<float> rpm = new List<float>();
        public List<float> SlowSignal = new List<float>();
        public List<float> Voltage = new List<float>();
    }
    /// <summary>
    /// 加速度
    /// </summary>
    public class ACQSpeed
    {
        public int channelid;
        public List<float> totaldB=new List<float> ();
        public List<float> highdB=new List<float> ();
        public List<float> lowdB=new List<float> ();
        public List<float> velocityRMS=new List<float> ();
        public List<DateTime> insertTime=new List<DateTime> ();
        public List<float> displacementRMS=new List<float>();
        public List<float> rpm = new List<float>();
    }

    public class ACQSpeed_Field
    {
        public int channelid;
        public float Rpms;
        public float FieldValues;
        public DateTime date1;
    }


    public class OperateACQDB
    {
     
        //MyDb mydb_del = Mdl_Class.DelMydb;
        //MyDb mydb_insert = Mdl_Class.InsertMydb;
        //MyDb mydb_other = Mdl_Class.Mydb_other;


        //插入ACQ数据
        public int AddACQ(string tableName, ACQDB acqdb)
        {
            int i = 0;
            using (MyDb mydb_insert = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb_insert.Open();
                    byte[] b = Activator.CreateInstance<StructHelper>().Serialize(acqdb.dataPaket);
                    i = mydb_insert.ExecuteNonQuery_acq(tableName, b, acqdb);
                    mydb_insert.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("AddACQ(ACQDB acqdb)", ex.ToString());
                }
            }
            return i;
        }


        //插入ACQ数据
        public string AddACQ_ReturnID(string tableName, ACQDB acqdb)
        {
            string id = "";
            using (MyDb mydb_insert = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb_insert.Open();
                    byte[] b = Activator.CreateInstance<StructHelper>().Serialize(acqdb.dataPaket);
                    id = mydb_insert.ExecuteNonQuery_acq_return_ID(tableName, b, acqdb);
                    mydb_insert.Close();
                }
                catch (Exception ex)
                {
                    id = "";
                    Mdl_Class.WriteLog.dbErrLog("AddACQ(ACQDB acqdb)", ex.ToString());
                }
            }
            return id;
        }


        //根据条件查询ACQ数据
        public ACQDB GetACQById(int id)
        {
               ACQDB acq =null;

            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {

                    mydb.Open();
                    string sql = string.Format("select * from T_B_ACQ_Hour where  Id={0}", id);
             
                    mydb.ExecuteReader(sql);
                    if (mydb.DataReader.Read())
                    {
                        acq = new ACQDB();
                        acq.Id = Convert.ToInt64(mydb.DataReader["Id"]);
                        acq.acqId = mydb.DataReader["acqId"].ToString();
                        acq.acqName = mydb.DataReader["acqName"].ToString();

                        byte[] by1 = mydb.DataReader["dataPaket"] as byte[];
                        CurrentDataPacketAndFFTData dpk = Activator.CreateInstance<StructHelper>().DeSerialize<CurrentDataPacketAndFFTData>(by1);
                        acq.dataPaket = dpk;
                        acq.insertTime = Convert.ToDateTime(mydb.DataReader["insertTime"]);
                        acq.projectId = Convert.ToInt32(mydb.DataReader["projectId"]);

                        //if (taskid != 0)
                        //{
                        //    acq.taskId = Convert.ToInt32(mydb.DataReader["taskId"]);
                        //}


                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetACQById(int id)", ex.ToString());
                }
            }
            return acq;
        }

        // 插入ACQ数据
        //public int AddOtherACQ(string tableName, ACQDB acqdb)
        //{
        //    int i = 0;
        //    using (mydb_other)
        //    {
        //        try
        //        {
        //            mydb_other.Open();
        //            byte[] b = Activator.CreateInstance<StructHelper>().Serialize(acqdb.dataPaket);
        //            i = mydb_other.ExecuteNonQuery_acq(tableName, b, acqdb);
        //            mydb_other.Close();
        //        }
        //        catch (Exception ex)
        //        {
        //            Mdl_Class.WriteLog.dbErrLog("AddACQ(ACQDB acqdb)", ex.ToString());
        //        }
        //    }
        //    return i;
        //}

        //插入ACQ_Task数据
        public int AddACQ_Task(string tableName, ACQDB acqdb)
        {
            int i = 0;
            using (MyDb mydb_insert = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb_insert.Open();
                    byte[] b = Activator.CreateInstance<StructHelper>().Serialize(acqdb.dataPaket);
                    i = mydb_insert.ExecuteNonQuery_acq_task(tableName, b, acqdb);
                    mydb_insert.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("AddACQ(ACQDB acqdb)", ex.ToString());
                }
            }
            return i;
        }

        //根据条件查询ACQ数据
        public List<ACQDB> GetAllACQ(int prjid, int taskid, string tableName, ACQDB acqdb, DateTime dt1, DateTime dt2)
        {
            List<ACQDB> acqlist = new List<ACQDB>();

            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {

                    mydb.Open();
                    string sql = string.Format("select * from {0} where 1=1 and projectId={1}", tableName, prjid);
                    if (acqdb != null)
                    {
                        //if (acqdb.userId!=-1)
                        //{
                        //    sql = sql + string.Format(" and userId={0}", acqdb.userId);
                        //}
                        if (acqdb.acqId != "")
                        {
                            sql = sql + string.Format(" and acqId={0}", acqdb.acqId);
                        }
                        if (acqdb.acqName != "")
                        {
                            sql = sql + string.Format(" and acqName='{0}'", acqdb.acqName);
                        }
                        if (acqdb.insertTime != null)
                        {
                            sql = sql + string.Format(" and insertTime='{0}'", acqdb.insertTime);
                        }

                        if (acqdb.projectId != -1)
                        {
                            sql = sql + string.Format(" and projectId={0}", acqdb.projectId);
                        }
                        if (taskid != 0)
                        {
                            sql = sql + string.Format(" and taskId={0}", taskid);
                        }
                    }
                    if (dt1 != null)
                    {
                        sql = sql + string.Format(" and insertTime between '{0}' and '{1}'", dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"));
                    }

                    sql = sql + " order by Id";
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        ACQDB acq = new ACQDB();
                        acq.Id = Convert.ToInt64(mydb.DataReader["Id"]);
                        acq.acqId = mydb.DataReader["acqId"].ToString();
                        acq.acqName = mydb.DataReader["acqName"].ToString();

                        byte[] by1 = mydb.DataReader["dataPaket"] as byte[];
                        CurrentDataPacketAndFFTData dpk = Activator.CreateInstance<StructHelper>().DeSerialize<CurrentDataPacketAndFFTData>(by1);
                        acq.dataPaket = dpk;
                        acq.insertTime = Convert.ToDateTime(mydb.DataReader["insertTime"]);
                        acq.projectId = Convert.ToInt32(mydb.DataReader["projectId"]);

                        if (taskid != 0)
                        {
                            acq.taskId = Convert.ToInt32(mydb.DataReader["taskId"]);
                        }


                        acqlist.Add(acq);
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllACQ(ACQDB acqdb,DateTime dt1,DateTime dt2)", ex.ToString());
                }
            }
            return acqlist;
        }

        //根据条件查询ACQ数据分页
        public List<DisplacementDataInfo> GetAllACQByPage(int prjid,string tableName, DateTime dt1, DateTime dt2, long pagevalue, long pagesize,int channelid)
        {
            List<DisplacementDataInfo> acqlist = new List<DisplacementDataInfo>();

            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();
                    //mydb_other.Open();
                    string sql = "";
                    if (pagevalue==0)
                    {
                        pagevalue = 1;
                    }
                    if (dt1 != null)
                    {
                        if (channelid >= 0)
                        {
                            sql = string.Format("select top {0} * from {1} where id not in (select top {2} id from {3} where InsertTime  between '{4}' and '{5}' and ProjectId={6} and ChannelId={7}) and ProjectId={8}", pagesize, tableName, (pagevalue - 1) * pagesize, tableName, dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"), prjid, channelid+1,prjid);
                        }
                        else 
                        {
                            sql = string.Format("select top {0} * from {1} where id not in (select top {2} id from {3} where InsertTime  between '{4}' and '{5}' and ProjectId={6}) and ProjectId={7}", pagesize, tableName, (pagevalue - 1) * pagesize, tableName, dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"), prjid, prjid);
                        }
                    }
                    else 
                    {
                        if (channelid >= 0)
                        {
                            sql = string.Format("select top {0} * from {1} where id not in (select top {2} id from {3} where projectId={4} and ChannelId={5}) and ProjectId={6}", pagesize, tableName, (pagevalue - 1) * pagesize, tableName, prjid, channelid+1,prjid);
                        }
                        else 
                        {
                            sql = string.Format("select top {0} * from {1} where id not in (select top {2} id from {3} where ProjectId={4}) and ProjectId={5}", pagesize, tableName, (pagevalue - 1) * pagesize, tableName, prjid, prjid);
                        }
                    }
                    if (channelid>=0) 
                    {
                        sql= sql+ "and  ChannelId="+ (channelid+1).ToString();
                    }
                    if (dt1 != null)
                    {
                        sql = sql + string.Format(" and InsertTime between '{0}' and '{1}'", dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"));
                    }
                    sql = sql + " order by InsertTime";
            
                    mydb.ExecuteReader(sql);

                    while (mydb.DataReader.Read())
                    {


                        DisplacementDataInfo acq = new DisplacementDataInfo();
                        acq.Id = Convert.ToInt64(mydb.DataReader["Id"]);
                        acq.ChannelId =Convert.ToInt16( mydb.DataReader["ChannelId"].ToString());
                        acq.PK2PK = Convert.ToSingle(mydb.DataReader["PK2PK"].ToString());
                        acq.GapVoltage = Convert.ToSingle(mydb.DataReader["GapVoltage"].ToString());
                        acq.Amps = mydb.BytesToFloat(mydb.DataReader["Amps"] as byte[]);
                        acq.Phs= mydb.BytesToFloat(mydb.DataReader["Phs"] as byte[]);
                        acq.ZDs = mydb.BytesToFloat(mydb.DataReader["ZDs"] as byte[]);
                        acq.RoomMeanSquareValue= Convert.ToSingle(mydb.DataReader["RoomMeanSquareValue"].ToString());
                        acq.OneMultiplierAmp = Convert.ToSingle(mydb.DataReader["OneMultiplierAmp"].ToString());
                        acq.OneMultiplierPhase = Convert.ToSingle(mydb.DataReader["OneMultiplierPhase"].ToString());
                        acq.TWOMultiplierAmp = Convert.ToSingle(mydb.DataReader["TWOMultiplierAmp"].ToString());
                        acq.TWOMultiplierPhase = Convert.ToSingle(mydb.DataReader["TWOMultiplierPhase"].ToString());
                        acq.HalfMultiplierAmp = Convert.ToSingle(mydb.DataReader["HalfMultiplierAmp"].ToString());
                        acq.HalfMultiplierPhase = Convert.ToSingle(mydb.DataReader["HalfMultiplierPhase"].ToString());
                        acq.FreOption1 = Convert.ToSingle(mydb.DataReader["FreOption1"].ToString());
                        acq.FreOption2 = Convert.ToSingle(mydb.DataReader["FreOption2"].ToString());
                        acq.FreOption3 = Convert.ToSingle(mydb.DataReader["FreOption3"].ToString());
                        acq.AlarmFlag = Convert.ToByte(mydb.DataReader["AlarmFlag"].ToString());
                        acq.InsertTime = Convert.ToDateTime(mydb.DataReader["InsertTime"].ToString());
                        acq.ProjectId = Convert.ToInt16(mydb.DataReader["ProjectId"].ToString());
                       
                        int speedid = Mdl_Class.GetSpeedId(channelid);
                        float rpm_value = 0.0f;
                        switch (speedid)
                        {
                            case 1:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm1"]);
                                break;
                            case 2:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm2"]);
                                break;
                            case 3:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm3"]);
                                break;
                            case 4:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm4"]);
                                break;
                            case 5:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm5"]);
                                break;
                            case 6:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm6"]);
                                break;
                            default:
                                break;
                        }
                        acq.Rpm = rpm_value;

                        acqlist.Add(acq);
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllACQByPage(string tableName, ACQDB acqdb, DateTime dt1, DateTime dt2,int pagevalue,int pagesize)", ex.ToString());
                }
            }
            return acqlist;
        }


        /// <summary>
        /// 根据测点查询
        /// </summary>
        /// <param name="prjid"></param>
        /// <param name="tableName"></param>
        /// <param name="date"></param>
        /// <param name="channel1"></param>
        /// <param name="channel2"></param>
        /// <returns></returns>
        public Dictionary<int, DisplacementDataInfo> GetAllACQByTestPoint(int prjid, string tableName,DateTime date,int channel1,int channel2)
        {
            Dictionary<int, DisplacementDataInfo> acqlist = new Dictionary<int, DisplacementDataInfo>();
            DisplacementDataInfo channe11_value = new DisplacementDataInfo();
            DisplacementDataInfo channe12_value = new DisplacementDataInfo();
            acqlist.Add(channel1, channe11_value);
            acqlist.Add(channel2, channe12_value);
            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();
                    //mydb_other.Open();
                    string sql = string.Format("select * from {0} where ProjectId={1} and InsertTime='{2}' and (ChannelId={3} or ChannelId={4})",
                        tableName, prjid, date, channel1+1, channel2+1);
                     
                    sql = sql + " order by Id";

                    mydb.ExecuteReader(sql);

                 

                    while (mydb.DataReader.Read())
                    {


                        DisplacementDataInfo acq = new DisplacementDataInfo();
                        acq.Id = Convert.ToInt64(mydb.DataReader["Id"]);
                        acq.ChannelId = Convert.ToInt16(mydb.DataReader["ChannelId"].ToString());
                        acq.PK2PK = Convert.ToSingle(mydb.DataReader["PK2PK"].ToString());
                        acq.GapVoltage = Convert.ToSingle(mydb.DataReader["GapVoltage"].ToString());
                        acq.Amps = mydb.BytesToFloat(mydb.DataReader["Amps"] as byte[]);
                        acq.Phs = mydb.BytesToFloat(mydb.DataReader["Phs"] as byte[]);
                        acq.ZDs = mydb.BytesToFloat(mydb.DataReader["ZDs"] as byte[]);
                        acq.RoomMeanSquareValue = Convert.ToSingle(mydb.DataReader["RoomMeanSquareValue"].ToString());
                        acq.OneMultiplierAmp = Convert.ToSingle(mydb.DataReader["OneMultiplierAmp"].ToString());
                        acq.OneMultiplierPhase = Convert.ToSingle(mydb.DataReader["OneMultiplierPhase"].ToString());
                        acq.TWOMultiplierAmp = Convert.ToSingle(mydb.DataReader["TWOMultiplierAmp"].ToString());
                        acq.TWOMultiplierPhase = Convert.ToSingle(mydb.DataReader["TWOMultiplierPhase"].ToString());
                        acq.HalfMultiplierAmp = Convert.ToSingle(mydb.DataReader["HalfMultiplierAmp"].ToString());
                        acq.HalfMultiplierPhase = Convert.ToSingle(mydb.DataReader["HalfMultiplierPhase"].ToString());
                        acq.FreOption1 = Convert.ToSingle(mydb.DataReader["FreOption1"].ToString());
                        acq.FreOption2 = Convert.ToSingle(mydb.DataReader["FreOption2"].ToString());
                        acq.FreOption3 = Convert.ToSingle(mydb.DataReader["FreOption3"].ToString());
                        acq.AlarmFlag = Convert.ToByte(mydb.DataReader["AlarmFlag"].ToString());
                        acq.InsertTime = Convert.ToDateTime(mydb.DataReader["InsertTime"].ToString());
                        acq.ProjectId = Convert.ToInt16(mydb.DataReader["ProjectId"].ToString());

                        int speedid = Mdl_Class.GetSpeedId(acq.ChannelId);
                        float rpm_value = 0.0f;
                        switch (speedid)
                        {
                            case 1:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm1"]);
                                break;
                            case 2:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm2"]);
                                break;
                            case 3:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm3"]);
                                break;
                            case 4:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm4"]);
                                break;
                            case 5:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm5"]);
                                break;
                            case 6:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm6"]);
                                break;
                            default:
                                break;
                        }
                        acq.Rpm = rpm_value;

                        if (acqlist.ContainsKey(acq.ChannelId-1)) {
                            acqlist[acq.ChannelId-1] = acq;
                        }
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllACQByTestPoint()", ex.ToString());
                }
            }
            return acqlist;
        }



        /// <summary>
        /// 根据时间查询
        /// </summary>
        /// <param name="prjid"></param>
        /// <param name="tableName"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public Dictionary<int, DisplacementDataInfo> GetAllACQByDate(short prjid, string tableName, DateTime date)
        {
            Dictionary<int, DisplacementDataInfo> acqlist = new Dictionary<int, DisplacementDataInfo>();

            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();
                    //mydb_other.Open();
                    string sql = string.Format("select * from {0} where ProjectId={1} and InsertTime='{1}'",
                        tableName, date);

                    sql = sql + " order by Id";

                    mydb.ExecuteReader(sql);



                    while (mydb.DataReader.Read())
                    {


                        DisplacementDataInfo acq = new DisplacementDataInfo();
                        acq.Id = Convert.ToInt64(mydb.DataReader["Id"]);
                        acq.ChannelId = Convert.ToInt16(mydb.DataReader["ChannelId"].ToString());
                        acq.PK2PK = Convert.ToSingle(mydb.DataReader["PK2PK"].ToString());
                        acq.GapVoltage = Convert.ToSingle(mydb.DataReader["GapVoltage"].ToString());
                        acq.Amps = mydb.BytesToFloat(mydb.DataReader["Amps"] as byte[]);
                        acq.Phs = mydb.BytesToFloat(mydb.DataReader["Phs"] as byte[]);
                        acq.ZDs = mydb.BytesToFloat(mydb.DataReader["ZDs"] as byte[]);
                        acq.RoomMeanSquareValue = Convert.ToSingle(mydb.DataReader["RoomMeanSquareValue"].ToString());
                        acq.OneMultiplierAmp = Convert.ToSingle(mydb.DataReader["OneMultiplierAmp"].ToString());
                        acq.OneMultiplierPhase = Convert.ToSingle(mydb.DataReader["OneMultiplierPhase"].ToString());
                        acq.TWOMultiplierAmp = Convert.ToSingle(mydb.DataReader["TWOMultiplierAmp"].ToString());
                        acq.TWOMultiplierPhase = Convert.ToSingle(mydb.DataReader["TWOMultiplierPhase"].ToString());
                        acq.HalfMultiplierAmp = Convert.ToSingle(mydb.DataReader["HalfMultiplierAmp"].ToString());
                        acq.HalfMultiplierPhase = Convert.ToSingle(mydb.DataReader["HalfMultiplierPhase"].ToString());
                        acq.FreOption1 = Convert.ToSingle(mydb.DataReader["FreOption1"].ToString());
                        acq.FreOption2 = Convert.ToSingle(mydb.DataReader["FreOption2"].ToString());
                        acq.FreOption3 = Convert.ToSingle(mydb.DataReader["FreOption3"].ToString());
                        acq.AlarmFlag = Convert.ToByte(mydb.DataReader["AlarmFlag"].ToString());
                        acq.InsertTime = Convert.ToDateTime(mydb.DataReader["InsertTime"].ToString());
                        acq.ProjectId = Convert.ToInt16(mydb.DataReader["ProjectId"].ToString());

                        int speedid = Mdl_Class.GetSpeedId(acq.ChannelId);
                        float rpm_value = 0.0f;
                        switch (speedid)
                        {
                            case 1:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm1"]);
                                break;
                            case 2:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm2"]);
                                break;
                            case 3:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm3"]);
                                break;
                            case 4:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm4"]);
                                break;
                            case 5:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm5"]);
                                break;
                            case 6:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm6"]);
                                break;
                            default:
                                break;
                        }
                        acq.Rpm = rpm_value;

                        if (acqlist.ContainsKey(acq.ChannelId-1))
                        {
                            acqlist[acq.ChannelId-1] = acq;
                        }
                        else 
                        {
                            acqlist.Add(acq.ChannelId-1, acq);
                        }
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllACQByDate()", ex.ToString());
                }
            }
            return acqlist;
        }


        //根据条件查询ACQ数据分页
        public List<ACQDB> GetAllACQByPage_black(int prjid, int taskid, string tableName, ACQDB acqdb, DateTime dt1, DateTime dt2, long pagevalue, long pagesize)
        {
            List<ACQDB> acqlist = new List<ACQDB>();

            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();
                    //mydb_other.Open();
                    string sql = "";
                    if (dt1 != null)
                    {
                        sql = string.Format("select top {0} * from {1} where blackAcqId not in (select top {2} blackAcqId from {3} where insertTime  between '{4}' and '{5}' and projectId={6}) and projectId={7}", pagesize, tableName, (pagevalue - 1) * pagesize, tableName, dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"), prjid, prjid);

                    }
                    else
                    {
                        sql = string.Format("select top {0} * from {1} where blackAcqId not in (select top {2} blackAcqId from {3} where projectId={4}) and projectId={5}", pagesize, tableName, (pagevalue - 1) * pagesize, tableName, prjid, prjid);
                    }
                    if (acqdb != null)
                    {
                        //if (acqdb.userId != -1)
                        //{
                        //    sql = sql + string.Format(" and userId={0}", acqdb.userId);
                        //}
                        if (acqdb.acqId != "")
                        {
                            sql = sql + string.Format(" and acqId={0}", acqdb.acqId);
                        }
                        if (acqdb.acqName != "")
                        {
                            sql = sql + string.Format(" and acqName='{0}'", acqdb.acqName);
                        }
                        if (acqdb.insertTime != null)
                        {
                            sql = sql + string.Format(" and insertTime='{0}'", acqdb.insertTime);
                        }

                        if (acqdb.projectId != -1)
                        {
                            sql = sql + string.Format(" and projectId={0}", acqdb.projectId);
                        }
                        if (taskid != 0)
                        {
                            sql = sql + string.Format(" and taskId={0}", taskid);
                        }
                    }
                    if (dt1 != null)
                    {
                        sql = sql + string.Format(" and insertTime between '{0}' and '{1}'", dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"));
                    }
                    sql = sql + " order by blackAcqId,insertTime";

                    mydb.ExecuteReader(sql);

                    while (mydb.DataReader.Read())
                    {


                        ACQDB acq = new ACQDB();
                        acq.Id = Convert.ToInt64(mydb.DataReader["blackAcqId"]);
                        acq.acqId = mydb.DataReader["acqId"].ToString();
                        acq.acqName = mydb.DataReader["acqName"].ToString();

                        byte[] by1 = mydb.DataReader["dataPaket"] as byte[];
                        CurrentDataPacketAndFFTData dpk = Activator.CreateInstance<StructHelper>().DeSerialize<CurrentDataPacketAndFFTData>(by1);
                        acq.dataPaket = dpk;
                        acq.insertTime = Convert.ToDateTime(mydb.DataReader["insertTime"]);
                        acq.projectId = Convert.ToInt32(mydb.DataReader["projectId"]);
                        //acq.testItemId = Convert.ToInt32(mydb.DataReader["testItemId"]);
                        //acq.testPointId = Convert.ToInt32(mydb.DataReader["testPointId"]);
                        //acq.userId = Convert.ToInt32(mydb.DataReader["userId"]);

                        if (taskid != 0)
                        {
                            acq.taskId = Convert.ToInt32(mydb.DataReader["taskId"]);
                        }
                        //AddOtherACQ("T_B_ACQ_Hour", acq);
                        acqlist.Add(acq);
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllACQByPage(string tableName, ACQDB acqdb, DateTime dt1, DateTime dt2,int pagevalue,int pagesize)", ex.ToString());
                }
            }
            return acqlist;
        }


        //根据条件查询ACQ数据
        public List<ACQDB> GetAllACQByTime(int prjid, int taskid, string tableName, string acqid, DateTime dt1, DateTime dt2, int selectstate)
        {
            List<ACQDB> acqlist = new List<ACQDB>();

            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();

                    string sql1 = string.Format("select ID from {0} where 1=1 and projectId={1}", tableName, prjid);

                    if (acqid != "")
                    {
                        sql1 = sql1 + string.Format(" and acqId={0}", Convert.ToInt32(acqid));
                    }

                    if (selectstate == 1)//大于当前时间
                    {
                        sql1 = sql1 + string.Format(" and insertTime > '{0}'", dt1);
                    }
                    else if (selectstate == 2)//时间范围
                    {
                        sql1 = sql1 + string.Format(" and insertTime between '{0}' and '{1}'", dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"));
                    }

                    sql1 = sql1 + " order by insertTime";
                    mydb.ExecuteReader(sql1);
                    List<long> idlist = new List<long>();
                    while (mydb.DataReader.Read())
                    {
                        idlist.Add(Convert.ToInt64(mydb.DataReader["Id"]));
                    }

                    mydb.Close();

                    if(idlist.Count>0){

                        string sql = string.Format("select * from {0} where id={1}", tableName, idlist[0]);

    
                        string sql_id = "";
                        for (int i = 1; i < idlist.Count;i++ )
                        {
                        

                                if ((i + 1) % 200 == 0)
                                {
                                    sql_id = sql + sql_id + string.Format(" or Id = {0}", idlist[i]);

                                    mydb.Open();
                                    mydb.ExecuteReader(sql_id);
                                    while (mydb.DataReader.Read())
                                    {
                                        ACQDB acq = new ACQDB();
                                        acq.Id = Convert.ToInt64(mydb.DataReader["Id"]);
                                        acq.acqId = mydb.DataReader["acqId"].ToString();
                                        acq.acqName = mydb.DataReader["acqName"].ToString();

                                        byte[] by1 = mydb.DataReader["dataPaket"] as byte[];
                                        CurrentDataPacketAndFFTData dpk = Activator.CreateInstance<StructHelper>().DeSerialize<CurrentDataPacketAndFFTData>(by1);
                                        acq.dataPaket = dpk;
                                        acq.insertTime = Convert.ToDateTime(mydb.DataReader["insertTime"]);
                                        acq.projectId = Convert.ToInt32(mydb.DataReader["projectId"]);
                                        //acq.testItemId = Convert.ToInt32(mydb.DataReader["testItemId"]);
                                        //acq.testPointId = Convert.ToInt32(mydb.DataReader["testPointId"]);
                                        //acq.userId = Convert.ToInt32(mydb.DataReader["userId"]);

                                        if (taskid != 0)
                                        {
                                            acq.taskId = Convert.ToInt32(mydb.DataReader["taskId"]);
                                        }
                                        acqlist.Add(acq);
                                    }
                                    mydb.Close();
                                    sql_id = "";
                                    i++;
                                    sql = string.Format("select * from {0} where id={1}", tableName, idlist[i]);
                                }
                                else
                                {
                                    sql_id = sql_id + string.Format(" or Id = {0}", idlist[i]);
                                }

                            
                
                        }

                        if (sql_id!="")
                        {
                            sql_id = sql + sql_id + " order by insertTime";
                            mydb.Open();
                            mydb.ExecuteReader(sql_id);
                            while (mydb.DataReader.Read())
                            {
                                ACQDB acq = new ACQDB();
                                acq.Id = Convert.ToInt64(mydb.DataReader["Id"]);
                                acq.acqId = mydb.DataReader["acqId"].ToString();
                                acq.acqName = mydb.DataReader["acqName"].ToString();

                                byte[] by1 = mydb.DataReader["dataPaket"] as byte[];
                                CurrentDataPacketAndFFTData dpk = Activator.CreateInstance<StructHelper>().DeSerialize<CurrentDataPacketAndFFTData>(by1);
                                acq.dataPaket = dpk;
                                acq.insertTime = Convert.ToDateTime(mydb.DataReader["insertTime"]);
                                acq.projectId = Convert.ToInt32(mydb.DataReader["projectId"]);
                                //acq.testItemId = Convert.ToInt32(mydb.DataReader["testItemId"]);
                                //acq.testPointId = Convert.ToInt32(mydb.DataReader["testPointId"]);
                                //acq.userId = Convert.ToInt32(mydb.DataReader["userId"]);

                                if (taskid != 0)
                                {
                                    acq.taskId = Convert.ToInt32(mydb.DataReader["taskId"]);
                                }
                                acqlist.Add(acq);
                            }
                            mydb.Close();
                        }
                    }
                    //mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllACQByPage(string tableName, ACQDB acqdb, DateTime dt1, DateTime dt2,int pagevalue,int pagesize)", ex.ToString());
                }
            }
            return acqlist;
        }



        


        //根据ACQID查询ACQ数据(分页)
        public Dictionary<int, List<DisplacementDataInfo>> GetACQByACQID(int prjid, int taskid, string tableName, int acqid, DateTime dt1, DateTime dt2, long pagevalue, long pagesize)
        {
            Dictionary<int, List<DisplacementDataInfo>> acqlist = new Dictionary<int, List<DisplacementDataInfo>>();

            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();
                    //mydb_other.Open();
                    string sql = "";
                    if (dt1 != null)
                    {
                   
                            sql = string.Format("select top {0} * from {1} where id not in (select top {2} id from {3} where InsertTime  between '{4}' and '{5}' and ProjectId={6}) and ProjectId={7}", pagesize*32, tableName, (pagevalue - 1) * pagesize*32, tableName, dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"), prjid, prjid);
                        
                    }
                    else
                    {
                    
                            sql = string.Format("select top {0} * from {1} where id not in (select top {2} id from {3} where ProjectId={4}) and ProjectId={5}", pagesize*32, tableName, (pagevalue - 1) * pagesize*32, tableName, prjid, prjid);
                        
                    }
                    sql = sql + string.Format(" and InsertTime between '{0}' and '{1}'", dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"));
                    sql = sql + " order by Id";

                    mydb.ExecuteReader(sql);



                    while (mydb.DataReader.Read())
                    {


                        DisplacementDataInfo acq = new DisplacementDataInfo();
                        acq.Id = Convert.ToInt64(mydb.DataReader["Id"]);
                        acq.ChannelId = Convert.ToInt16(mydb.DataReader["ChannelId"].ToString());
                        acq.PK2PK = Convert.ToSingle(mydb.DataReader["PK2PK"].ToString());
                        acq.GapVoltage = Convert.ToSingle(mydb.DataReader["GapVoltage"].ToString());
                        acq.Amps = mydb.BytesToFloat(mydb.DataReader["Amps"] as byte[]);
                        acq.Phs = mydb.BytesToFloat(mydb.DataReader["Phs"] as byte[]);
                        acq.ZDs = mydb.BytesToFloat(mydb.DataReader["ZDs"] as byte[]);
                        acq.RoomMeanSquareValue = Convert.ToSingle(mydb.DataReader["RoomMeanSquareValue"].ToString());
                        acq.OneMultiplierAmp = Convert.ToSingle(mydb.DataReader["OneMultiplierAmp"].ToString());
                        acq.OneMultiplierPhase = Convert.ToSingle(mydb.DataReader["OneMultiplierPhase"].ToString());
                        acq.TWOMultiplierAmp = Convert.ToSingle(mydb.DataReader["TWOMultiplierAmp"].ToString());
                        acq.TWOMultiplierPhase = Convert.ToSingle(mydb.DataReader["TWOMultiplierPhase"].ToString());
                        acq.HalfMultiplierAmp = Convert.ToSingle(mydb.DataReader["HalfMultiplierAmp"].ToString());
                        acq.HalfMultiplierPhase = Convert.ToSingle(mydb.DataReader["HalfMultiplierPhase"].ToString());
                        acq.FreOption1 = Convert.ToSingle(mydb.DataReader["FreOption1"].ToString());
                        acq.FreOption2 = Convert.ToSingle(mydb.DataReader["FreOption2"].ToString());
                        acq.FreOption3 = Convert.ToSingle(mydb.DataReader["FreOption3"].ToString());
                        acq.AlarmFlag = Convert.ToByte(mydb.DataReader["AlarmFlag"].ToString());
                        acq.InsertTime = Convert.ToDateTime(mydb.DataReader["InsertTime"].ToString());
                        acq.ProjectId = Convert.ToInt16(mydb.DataReader["ProjectId"].ToString());

                        int speedid = Mdl_Class.GetSpeedId(acq.ChannelId);
                        float rpm_value = 0.0f;
                        switch (speedid)
                        {
                            case 1:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm1"]);
                                break;
                            case 2:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm2"]);
                                break;
                            case 3:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm3"]);
                                break;
                            case 4:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm4"]);
                                break;
                            case 5:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm5"]);
                                break;
                            case 6:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm6"]);
                                break;
                            default:
                                break;
                        }
                        acq.Rpm = rpm_value;

                        if (acqlist.ContainsKey(acq.ChannelId-1))
                        {
                            acqlist[acq.ChannelId-1].Add(acq);
                        }
                        else
                        {
                            List<DisplacementDataInfo> dislist = new List<DisplacementDataInfo>();
                            dislist.Add(acq);
                            acqlist.Add(acq.ChannelId-1, dislist);
                        }
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllACQByDate()", ex.ToString());
                }
            }
            return acqlist;
        }

        //根据ACQID查询ACQ数据(总数量)
        public long GetACQByACQIDPageSize(int prjid, string tableName, DateTime dt1, DateTime dt2)
        {
            long pagecount = 0;

            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("select count(*) as pagecount from {0} where 1=1 and ProjectId={1}", tableName, prjid);

            
                    if (dt1 != null)
                    {
                        sql = sql + string.Format(" and insertTime between '{0}' and '{1}'", dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"));
                    }
                 
                    mydb.ExecuteReader(sql);
                    if (mydb.DataReader.Read())
                    {

                        pagecount = Convert.ToInt64(mydb.DataReader["pagecount"])/32;

                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetACQByACQIDPageSize(string tableName,int acqid, DateTime dt1, DateTime dt2)", ex.ToString());
                }
            }
            return pagecount;
        }


        //根据条件查询ACQ数据获取总数量
        public long GetAllACQPageSum(int prjid, string tableName, DateTime dt1, DateTime dt2,int channelid)
        {
            //List<ACQDB> acqlist = new List<ACQDB>();
            long pagesum = 0;
            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("select count(Id) as pagecount from {0} where 1=1 and projectId={1}", tableName, prjid);


                    if (channelid >= 0)
                    {
                        sql = sql + string.Format(" and ChannelId={0}", channelid);
                    }
                    if (dt1 != null)
                    {
                        sql = sql + string.Format(" and insertTime between '{0}' and '{1}'", dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"));
                    }
                
                    mydb.ExecuteReader(sql);
                    if (mydb.DataReader.Read())
                    {
                        pagesum = Convert.ToInt64(mydb.DataReader["pagecount"]); ;
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog(" GetAllACQPageSum()", ex.ToString());
                }
            }
            return pagesum;
        }

        //导入数据
        public bool ImportDataBase(string tableName, List<ACQDB> acqdblist, int minid, int maxid, int projectid, string acqid)
        {
            bool yorn = true;
            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();
                    //mydb.BeginTransaction();


                    List<byte[]> btlist = new List<byte[]>();
                    List<ACQDB> acqlist = new List<ACQDB>();
                    string del_sql = "";
                    int delcount = 0;
                    int sql_delcount = 0;
         
                    if ((maxid - minid) % 500 == 0)
                    {
                        sql_delcount = (maxid - minid) / 500;
                    }
                    else
                    {
                        sql_delcount = (maxid - minid) / 500 + 1;
                    }
                    for (int i = 0; i < sql_delcount; i++)
                    {
                        del_sql = string.Format("delete top(500) from {0} where acqId='{1}' and projectId={2} and id>={3} and id<={4}", tableName, acqid, projectid, minid, maxid);
                        delcount = mydb.ExecuteNonQuery(del_sql);
                        if (i == sql_delcount - 1)
                        {
                            Mdl_Class.nowupdatecount = Mdl_Class.nowupdatecount + (maxid - minid) % 500;
                        }
                        else
                        {
                            Mdl_Class.nowupdatecount = Mdl_Class.nowupdatecount + 500;
                        }
                        if (delcount < 0)
                        {
                            break;
                        }
                    }
                    int addcount = 50;//每次批量插入的条数
                    if (delcount >= 0)
                    {
                        for (int i = 0; i < acqdblist.Count; i++)
                        {

                            byte[] b = Activator.CreateInstance<StructHelper>().Serialize(acqdblist[i].dataPaket);


                            if (acqdblist.Count - i < addcount)
                            {
                                int updatacount = mydb.ExecuteNonQuery_acq(tableName, b, acqdblist[i]);
                                Mdl_Class.nowupdatecount = Mdl_Class.nowupdatecount + 1;
                                if (updatacount <= 0)
                                {
                                    yorn = false;
                                    break;

                                }
                            }
                            else if ((i + 1) % addcount == 0)
                            {
                                btlist.Add(b);
                                acqlist.Add(acqdblist[i]);
                                int updatacount = mydb.ExecuteNonQuery_acq_batch(tableName, btlist, acqlist, addcount);
                                Mdl_Class.nowupdatecount = Mdl_Class.nowupdatecount + addcount;
                                if (updatacount <= 0)
                                {
                                    yorn = false;
                                    break;
                                }
                                btlist.Clear();
                                acqlist.Clear();
                            }
                            else
                            {
                                btlist.Add(b);
                                acqlist.Add(acqdblist[i]);
                            }

                        }
                    }
                    else
                    {
                        yorn = false;
                    }

                    //if (yorn)
                    //{
                    //    mydb.Commit();

                    //}
                    //else
                    //{
                    //    mydb.Rollback();
                    //}

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    yorn = false;
                    Mdl_Class.WriteLog.dbErrLog("ImportDataBase(string tableName,DataTable dt1)", ex.ToString());
                }
            }
            //Thread.Sleep(500);
            return yorn;
        }



        //导入数据
        public bool ImportDataBase_New(string tableName, List<ACQDB> acqdblist,string key,int speedid,int slowid,double xs,double pyl,double lmd)
        {
            bool yorn = true;
            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();

               
                    for (int i = 0; i < acqdblist.Count; i++)
                    {
                        string importlog = "";
                        byte[] b = Activator.CreateInstance<StructHelper>().Serialize(acqdblist[i].dataPaket);
                        int updatacount = mydb.ExecuteNonQuery_acq(tableName, b, acqdblist[i]);
                        if (updatacount <= 0)
                        {
                            yorn = false;
                            break;
                        }
                        else
                        {
                            Mdl_Class.nowupdatecount = Mdl_Class.nowupdatecount + 1;
                            string rpm="";
                            switch (speedid)
	                        {
                                   case 0:
                                    rpm=acqdblist[i].dataPaket.CurrentdataPacket.Rpm1.ToString();
                                    break;
                                    case 1:
                                     rpm=acqdblist[i].dataPaket.CurrentdataPacket.Rpm2.ToString();
                                    break;
                                    case 2:
                                     rpm=acqdblist[i].dataPaket.CurrentdataPacket.Rpm3.ToString();
                                    break;
                                    case 3:
                                     rpm=acqdblist[i].dataPaket.CurrentdataPacket.Rpm4.ToString();
                                    break;
                                default:
                                    rpm="";
                                    break;
                            }
                            double mxh = 0.0;
                            if (slowid == 0)
                            {
                                mxh = Math.Round(Convert.ToSingle(((acqdblist[i].dataPaket.CurrentdataPacket.SlowSignal[slowid] - 32768) / 32768) * 29.3 * xs), 1);
                            }
                            else if (slowid == 1)
                            {
                                mxh = Math.Round(Convert.ToSingle((acqdblist[i].dataPaket.CurrentdataPacket.SlowSignal[slowid] / lmd) + pyl), 1);
                            }
                            else
                            {
                                mxh = Math.Round(Convert.ToSingle(acqdblist[i].dataPaket.CurrentdataPacket.SlowSignal[slowid] / lmd), 1);
                            }
                            importlog = string.Format("{0},{1},{2}", acqdblist[i].insertTime, rpm, mxh.ToString());

                            Mdl_Class.WriteLog.Import(key, importlog);
                        }

                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    yorn = false;
                    Mdl_Class.WriteLog.dbErrLog("ImportDataBase(string tableName,DataTable dt1)", ex.ToString());
                }
            }
            //Thread.Sleep(500);
            return yorn;
        }

        //查询导入的数据是否存在
        public bool ImportDataExistence(string tablename, DateTime importdata, int projectid, string acqid,int channelid)
        {
            bool yorn = false;
            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("select id from {0} where projectId={1} and acqId='{2}' and insertTime='{3}'", tablename, projectid, acqid, importdata.ToString("yyyy-MM-dd HH:mm:ss"));
                    if (channelid!=-1)
                    {
                        sql = sql + string.Format(" and channelId={0}", channelid+1);
                    }
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        yorn = true;
                        break;
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog(" ImportDataExistence(string importdata) ", ex.ToString());
                }
            }
            return yorn;
        }

        //根据时间查询ID(min)
        public int ImportDataIDMinID(string tablename, DateTime importdata, int projectid, string acqid)
        {
            int id = 0;
            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("select Min(id) as minid from {0} where projectId={1} and acqId='{2}' and insertTime='{3}'", tablename, projectid, acqid, importdata);
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        id = Convert.ToInt32(mydb.DataReader["minid"]);
                        break;
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("ImportDataIDMinID(string tablename, DateTime importdata, int projectid, string acqid)", ex.ToString());
                }
            }
            return id;
        }


        //根据时间查询ID(Max)
        public int ImportDataIDMaxID(string tablename, DateTime importdata, int projectid, string acqid)
        {
            int id = 0;
            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("select Max(id) as maxid from {0} where projectId={1} and acqId='{2}' and insertTime='{3}'", tablename, projectid, acqid, importdata);
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        id = Convert.ToInt32(mydb.DataReader["maxid"]);
                        break;
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog(" ImportDataIDMaxID(string tablename, DateTime importdata, int projectid, string acqid) ", ex.ToString());
                }
            }
            return id;
        }


        //查询DataPaket是否存在
        //public bool SelectACQExistence(short[] dtime, string tablename, string acqid, int projectid)
        //{
        //    //List<ACQDB> acqlist = new List<ACQDB>();
        //    MyDb mydb_select = new MyDb(Mdl_Class.Systemset.DBConnectionString());
        //    bool yorn = false;
        //    using (mydb_select)
        //    {
        //        try
        //        {
        //            mydb_select.Open();
        //            string dtime1 = dtime[0].ToString() + "/" + dtime[1].ToString() + "/" + dtime[2].ToString() + " " + dtime[3].ToString() + ":" + dtime[4].ToString() + ":" + dtime[5].ToString();

        //            string sql = string.Format("select count(*) as pagecount from {0} where 1=1 and projectId={1} and acqId='{1}' and insertTime='{2}'", tablename, projectid, acqid, dtime1);


        //            mydb_select.ExecuteReader(sql);
        //            while (mydb_select.DataReader.Read())
        //            {
        //                yorn = true;
        //            }
        //            mydb_select.Close();
        //        }
        //        catch (Exception ex)
        //        {
        //            Mdl_Class.WriteLog.dbErrLog("SelectACQExistence(short[] dtime, string tablename, string acqid, int projectid)", ex.ToString());
        //        }
        //    }
        //    return yorn;
        //}


        public int Delete_ACQ()
        {    int delcount = 0;
          

     
        
          
            using (MyDb mydb_del = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    string sql_hh = string.Format("delete from T_B_ACQ_Hour where id<(select Max(id)-180000 from T_B_ACQ_Hour)");
                    string sql_dd = string.Format("delete from T_B_ACQ_Day where id<(select Max(id)-140000 from T_B_ACQ_Day)");
                    string sql_wk = string.Format("delete from T_B_ACQ_Week where id<(select Max(id)-140000 from T_B_ACQ_Week)");
                    string sql_mm = string.Format("delete from T_B_ACQ_Month where id<(select Max(id)-140000 from T_B_ACQ_Month)");
                    string sql_yy = string.Format("delete from T_B_ACQ_Year where id<(select Max(id)-43000 from T_B_ACQ_Year)");
                    string sql_speed = string.Format("delete from T_B_ACQ_Acceleration where id<(select Max(id)-140000 from T_B_ACQ_Acceleration)");
                    mydb_del.Open();
                    delcount = mydb_del.ExecuteNonQuery(sql_hh);
                    delcount = mydb_del.ExecuteNonQuery(sql_dd);
                    delcount = mydb_del.ExecuteNonQuery(sql_wk);
                    delcount = mydb_del.ExecuteNonQuery(sql_mm);
                    delcount = mydb_del.ExecuteNonQuery(sql_yy);
                    delcount = mydb_del.ExecuteNonQuery(sql_speed);
                    mydb_del.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog(" Delete_ACQ(string dataType)", ex.ToString());
                }
            }
     
            return delcount;
        }

        /// <summary>
        /// 通过ID删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelACQ(List<long> idlsit,string tablename) 
        {
            int delcount = 0;

            using (MyDb mydb_del = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb_del.Open();
                    mydb_del.BeginTransaction();
                    string sql = string.Format("delete from {0}", tablename);
                    for (int i = 0; i < idlsit.Count;i++ )
                    {
                        if (i > 1000)
                        {
                            delcount = mydb_del.ExecuteNonQuery(sql);
                            sql = string.Format("delete from {0} where Id={1}", tablename, idlsit[i]);
                        }
                        else
                        {
                            if (i == 0)
                            {
                                sql = sql + string.Format(" where Id={0}", idlsit[i]);
                            }
                            else
                            {
                                sql = sql + string.Format(" or Id={0}", idlsit[i]);
                            }
                        }
                   
                    }
                    delcount = mydb_del.ExecuteNonQuery(sql);
                    if (delcount > 0)
                    {
                        mydb_del.Commit();
                    }
                    else 
                    {
                        mydb_del.Rollback();
                    }
        
                    mydb_del.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("DelACQ(List<int> idlsit,string tablename)", ex.ToString());
                }
            }
            return delcount;
        }





        //根据条件查询ACQ数据
        public Dictionary<int, ACQData> GetAllACQDataByTime(List<int> channelidlist, int prjid, string tableName, DateTime dt1, DateTime dt2, int selectstate)
        {

            Dictionary<int, ACQData> channelToACQ = new Dictionary<int, ACQData>();
            //Mdl_Class.speedtotimelist.Clear();
            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();

                    TimeSpan ts1 = new TimeSpan(dt1.Ticks);
                    TimeSpan ts2 = new TimeSpan(dt2.Ticks);
                        TimeSpan ts3 = ts1.Subtract(ts2).Duration();
                        if (ts3.TotalHours > 12)
                        {
                            tableName = "View_Displacement_Day";
                        }
                        else 
                        {
                            tableName = "View_Displacement";
                        }
                    string sql = string.Format("select * from {0} where 1=1 and ProjectId={1}", tableName, prjid);
      
                
       
                    if (selectstate == 1)//大于当前时间
                    {
                        sql = sql + string.Format(" and insertTime > '{0}'", dt1);
                    }
                    else if (selectstate == 2)//时间范围
                    {
                        sql = sql + string.Format(" and InsertTime between '{0}' and '{1}'", dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"));
                    }


                    for (int i = 0; i < channelidlist.Count; i++)
                    {
                        if (i == 0 && channelidlist.Count > 1)
                        {
                            sql = sql + string.Format(" and (ChannelId={0}", channelidlist[i]+1);
                        }
                        else if (i == 0 && channelidlist.Count == 1)
                        {
                            sql = sql + string.Format(" and ChannelId={0}", channelidlist[i]+1);
                        }
                        else if (i > 0 && i == channelidlist.Count - 1)
                        {
                            sql = sql + string.Format(" or ChannelId={0})", channelidlist[i]+1);
                        }
                        else if (i > 0 && channelidlist.Count > 1)
                        {
                            sql = sql + string.Format(" or ChannelId={0}", channelidlist[i]+1);
                        }
                    }
             
                    sql = sql + " order by Id";
                    mydb.ExecuteReader(sql);
                    ACQData acq = null;
                    SpeedToTime sp=null;
                   
                    while (mydb.DataReader.Read())
                    {
                        
                        int channelid = Convert.ToInt32(mydb.DataReader["ChannelId"]);
                        if (!channelToACQ.ContainsKey(channelid-1))
                        {
                    
                            ACQData acq1 = new ACQData();
                            acq1.channelid = channelid-1;
                            channelToACQ.Add(channelid-1, acq1);
                        }
                        acq = channelToACQ[channelid-1];
                        int speedid = Mdl_Class.GetSpeedId(channelid-1);
                        float rpm_value = 0.0f;
                        switch (speedid)
                        {
                            case 1:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm1"]);
                                break;
                            case 2:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm2"]);
                                break;
                            case 3:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm3"]);
                                break;
                            case 4:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm4"]);
                                break;
                            case 5:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm5"]);
                                break;
                            case 6:
                                rpm_value = Convert.ToSingle(mydb.DataReader["Rpm6"]);
                                break;
                            default:
                                break;
                        }

                        float GapVoltage = Convert.ToSingle(mydb.DataReader["GapVoltage"]);
                        float PK2PK = Convert.ToSingle(mydb.DataReader["PK2PK"]);
                        float OneMultiplierAmp = Convert.ToSingle(mydb.DataReader["OneMultiplierAmp"]);
                        float OneMultiplierPhase = Convert.ToSingle(mydb.DataReader["OneMultiplierPhase"]);
                        float TWOMultiplierAmp = Convert.ToSingle(mydb.DataReader["TWOMultiplierAmp"]);
                        float TWOMultiplierPhase = Convert.ToSingle(mydb.DataReader["TWOMultiplierPhase"]);
                        float RoomSquareValue = Convert.ToSingle(mydb.DataReader["RoomMeanSquareValue"]);
                        DateTime insertTime = Convert.ToDateTime(mydb.DataReader["InsertTime"]);
                        acq.rpm.Add(rpm_value);
                        acq.GapVoltage.Add(GapVoltage);
                        acq.PK2PK.Add(PK2PK);
                        acq.OneMultiplierAmp.Add(OneMultiplierAmp);
                        acq.TWOMultiplierAmp.Add(TWOMultiplierAmp);
                        acq.RoomSquareValue.Add(RoomSquareValue);
                        acq.OneMultiplierPhase.Add(OneMultiplierPhase);
                        acq.TWOMultiplierPhase.Add(TWOMultiplierPhase);
                        acq.insertTime.Add(insertTime);
                    }
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllACQByPage(string tableName, ACQDB acqdb, DateTime dt1, DateTime dt2,int pagevalue,int pagesize)", ex.ToString());
                }
            }
            return channelToACQ;
        }



        //根据条件查询ACQ数据
        public void GetAllACQDataByField(ref Dictionary<int, List<ACQData_Field>> channelToACQ ,int channelId, int prjid, string tableName, DateTime dt1, DateTime dt2, string fieldName)
        {
            //Mdl_Class.speedtotimelist.Clear();
            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();

                    TimeSpan ts1 = new TimeSpan(dt1.Ticks);
                    TimeSpan ts2 = new TimeSpan(dt2.Ticks);
                    TimeSpan ts3 = ts1.Subtract(ts2).Duration();
                    if (ts3.TotalHours > 12)
                    {
                        tableName = "View_Displacement_Day";
                    }
                    else
                    {
                        tableName = "View_Displacement";
                    }
                    int speedid = Mdl_Class.GetSpeedId(channelId);
                    string rpm_fieldName = "Rpm1";
                    switch (speedid)
                    {
                        case 1:
                            rpm_fieldName = "Rpm1";
                            break;
                        case 2:
                            rpm_fieldName = "Rpm2";
                            break;
                        case 3:
                            rpm_fieldName = "Rpm3";
                            break;
                        case 4:
                            rpm_fieldName = "Rpm4";
                            break;
                        case 5:
                            rpm_fieldName = "Rpm5";
                            break;
                        case 6:
                            rpm_fieldName = "Rpm6";
                            break;
                        default:
                            break;
                    }

                    string sql = string.Format("select ChannelId,{0},{1},InsertTime from {2} where  ProjectId={3}", fieldName, rpm_fieldName,tableName, prjid);
                    if (dt1!=null) {
                        sql = sql + string.Format(" and InsertTime between '{0}' and '{1}'", dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"));
                    }
                    if (channelId >=0)
                    {
                        sql = sql + string.Format(" and ChannelId={0}", channelId+1);
                    }
                    sql = sql + " order by Id";
                    mydb.ExecuteReader(sql);
                    ACQData acq = null;
                    SpeedToTime sp = null;
                    List<ACQData_Field> acqdatalist = new List<ACQData_Field>();
              
                    List<string> datetimelist = new List<string>();
                    while (mydb.DataReader.Read())
                    {
                        if (!datetimelist.Contains(Convert.ToDateTime(mydb.DataReader["InsertTime"]).ToString("yyyy-MM-dd HH:mm:ss"))) {

                            datetimelist.Add(Convert.ToDateTime(mydb.DataReader["InsertTime"]).ToString("yyyy-MM-dd HH:mm:ss"));
                                ACQData_Field acq_field = new ACQData_Field();
                                acq_field.channelid = Convert.ToInt32(mydb.DataReader["ChannelId"]);
                                acq_field.Rpms = Convert.ToSingle(mydb.DataReader[rpm_fieldName]);
                                acq_field.FieldValues = Convert.ToSingle(mydb.DataReader[fieldName]);
                                acq_field.date1 = Convert.ToDateTime(mydb.DataReader["InsertTime"]);

                                switch (rpm_fieldName)
                                {
                                    case "OneMultiplierAmp":
                                        acq_field.FieldValues2 = Convert.ToSingle(mydb.DataReader["OneMultiplierPhase"]);
                                        break;
                                    case "TWOMultiplierAmp":
                                        acq_field.FieldValues2 = Convert.ToSingle(mydb.DataReader["TWOMultiplierPhase"]);
                                        break;
                                    case "HalfMultiplierAmp":
                                        acq_field.FieldValues2 = Convert.ToSingle(mydb.DataReader["HalfMultiplierPhase"]);
                                        break;
                                    default:
                                        break;
                                }

                                acqdatalist.Add(acq_field);

                        }
                    }
                    if (channelToACQ.ContainsKey(channelId))
                    {
                        channelToACQ[channelId] = acqdatalist;

                    }
                    else 
                    {
                        channelToACQ.Add(channelId, acqdatalist);
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllACQDataByField()", ex.ToString());
                }
            }
            //return channelToACQ;
        }


        //根据条件查询ACQ数据
        public Dictionary<int, ACQSlow> GetAllACQSlowByTime(int prjid, string tableName, DateTime dt1, DateTime dt2, int selectstate)
        {

            Dictionary<int, ACQSlow> channelToACQ = new Dictionary<int, ACQSlow>();
            //Mdl_Class.speedtotimelist.Clear();
            using (MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString()))
            {
                try
                {
                    mydb.Open();
                    tableName = "View_Slow";
                    string sql = string.Format("select * from {0} where 1=1 and ProjectId={1}", tableName, prjid);



                    if (selectstate == 1)//大于当前时间
                    {
                        sql = sql + string.Format(" and insertTime > '{0}'", dt1);
                    }
                    else if (selectstate == 2)//时间范围
                    {
                        sql = sql + string.Format(" and InsertTime between '{0}' and '{1}'", dt1.ToString("yyyy-MM-dd HH:mm:ss"), dt2.ToString("yyyy-MM-dd HH:mm:ss"));
                    }


        
                    sql = sql + " order by SlowId";
                    mydb.ExecuteReader(sql);
                    ACQSlow acq1 = new ACQSlow() ;
                    ACQSlow acq2 = new ACQSlow();
                    ACQSlow acq3 = new ACQSlow();
                    ACQSlow acq4 = new ACQSlow();
                    ACQSlow acq5 = new ACQSlow();
                    ACQSlow acq6 = new ACQSlow();
                    List<string> datetimelist = new List<string>();
                    while (mydb.DataReader.Read())
                    {
                        if (!datetimelist.Contains(Convert.ToDateTime(mydb.DataReader["InsertTime"]).ToString("yyyy-MM-dd HH:mm:ss")))
                        {

                            datetimelist.Add(Convert.ToDateTime(mydb.DataReader["InsertTime"]).ToString("yyyy-MM-dd HH:mm:ss"));
                            int speedid = 1;
                            float rpm_value = 0.0f;
                            switch (speedid)
                            {
                                case 1:
                                    rpm_value = Convert.ToSingle(mydb.DataReader["Rpm1"]);
                                    break;
                                case 2:
                                    rpm_value = Convert.ToSingle(mydb.DataReader["Rpm2"]);
                                    break;
                                case 3:
                                    rpm_value = Convert.ToSingle(mydb.DataReader["Rpm3"]);
                                    break;
                                case 4:
                                    rpm_value = Convert.ToSingle(mydb.DataReader["Rpm4"]);
                                    break;
                                case 5:
                                    rpm_value = Convert.ToSingle(mydb.DataReader["Rpm5"]);
                                    break;
                                case 6:
                                    rpm_value = Convert.ToSingle(mydb.DataReader["Rpm6"]);
                                    break;
                                default:
                                    break;
                            }

                            float SlowSignal1 = Convert.ToSingle(mydb.DataReader["SlowSignal1"]);
                            float Voltage1 = Convert.ToSingle(mydb.DataReader["Voltage1"]);
                            float SlowSignal2 = Convert.ToSingle(mydb.DataReader["SlowSignal2"]);
                            float Voltage2 = Convert.ToSingle(mydb.DataReader["Voltage2"]);
                            float SlowSignal3 = Convert.ToSingle(mydb.DataReader["SlowSignal3"]);
                            float Voltage3 = Convert.ToSingle(mydb.DataReader["Voltage3"]);
                            float SlowSignal4 = Convert.ToSingle(mydb.DataReader["SlowSignal4"]);
                            float Voltage4 = Convert.ToSingle(mydb.DataReader["Voltage4"]);
                            float SlowSignal5 = Convert.ToSingle(mydb.DataReader["SlowSignal5"]);
                            float Voltage5 = Convert.ToSingle(mydb.DataReader["Voltage5"]);
                            float SlowSignal6 = Convert.ToSingle(mydb.DataReader["SlowSignal6"]);
                            float Voltage6 = Convert.ToSingle(mydb.DataReader["Voltage6"]);
                            DateTime insertTime = Convert.ToDateTime(mydb.DataReader["InsertTime"]);
                            acq1.rpm.Add(rpm_value);
                            acq1.insertTime.Add(insertTime);
                            acq1.id = 0;
                            acq1.SlowSignal.Add(SlowSignal1);
                            acq1.Voltage.Add(Voltage1);

                            acq2.rpm.Add(rpm_value);
                            acq2.insertTime.Add(insertTime);
                            acq2.id = 1;
                            acq2.SlowSignal.Add(SlowSignal2);
                            acq2.Voltage.Add(Voltage2);


                            acq3.rpm.Add(rpm_value);
                            acq3.insertTime.Add(insertTime);
                            acq3.id = 2;
                            acq3.SlowSignal.Add(SlowSignal3);
                            acq3.Voltage.Add(Voltage3);


                            acq4.rpm.Add(rpm_value);
                            acq4.insertTime.Add(insertTime);
                            acq4.id = 3;
                            acq4.SlowSignal.Add(SlowSignal4);
                            acq4.Voltage.Add(Voltage4);


                            acq5.rpm.Add(rpm_value);
                            acq5.insertTime.Add(insertTime);
                            acq5.id = 4;
                            acq5.SlowSignal.Add(SlowSignal5);
                            acq5.Voltage.Add(Voltage5);


                            acq6.rpm.Add(rpm_value);
                            acq6.insertTime.Add(insertTime);
                            acq6.id = 5;
                            acq6.SlowSignal.Add(SlowSignal6);
                            acq6.Voltage.Add(Voltage6);
                        }
            
                    }
                    channelToACQ.Add(0, acq1);
                    channelToACQ.Add(1, acq2);
                    channelToACQ.Add(2, acq3);
                    channelToACQ.Add(3, acq4);
                    channelToACQ.Add(4, acq5);
                    channelToACQ.Add(5, acq6);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllACQByPage(string tableName, ACQDB acqdb, DateTime dt1, DateTime dt2,int pagevalue,int pagesize)", ex.ToString());
                }
            }
            return channelToACQ;
        }
    }
}
